package com.example.springbootrbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.springbootrbac.entity.*;
import com.example.springbootrbac.repository.*;
import com.example.springbootrbac.service.UpdateService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Service
public class UpdateServiceImpl implements UpdateService {
    @Resource(name = "announcementRepository")
    AnnouncementRepository announcementRepository;
    @Resource(name = "roomRepository")
    RoomRepository roomRepository;
    @Resource(name = "courseRepository")
    CourseRepository courseRepository;
    @Resource(name = "messagesBoardRepository")
    MessagesBoardRepository messagesBoardRepository;
    @Resource(name = "teacherRepository")
    TeacherRepository teacherRepository;

    @Override
    public String updateAnnouncement(AnnouncementEntity announcement, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        UpdateWrapper<AnnouncementEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", announcement.getId())
                .set("title", announcement.getTitle())
                .set("text", announcement.getText())
                .set("update_by", user.getUsername())
                .set("update_time", LocalDateTime.now());
        Integer rows = announcementRepository.update(announcement, updateWrapper);
        if (!ObjectUtils.isEmpty(rows)) {
            return "token:更改成功！";
        }

        return null;
    }

    @Override
    public String updateRoom(RoomEntity room, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        UpdateWrapper<RoomEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", room.getId())
                .set("household", room.getHousehold())
                .set("quality", room.getQuality())
                .set("occupied", room.getOccupied())
                .set("available",room.getAvailable())
                .set("update_by",user.getUsername())
                .set("update_time", LocalDateTime.now());
        Integer rows = roomRepository.update(room, updateWrapper);
        if (!ObjectUtils.isEmpty(rows)) {
            return "token:更改成功！";
        }
        return null;
    }

    @Override
    public String updateCourse(CourseEntity course, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        //检测外码
        TeacherEntity test_teacher = teacherRepository.selectOne(
                new QueryWrapper<TeacherEntity>()
                        .eq("tname",course.getTeacher())
        );
        RoomEntity test_room = roomRepository.selectOne(
                new QueryWrapper<RoomEntity>()
                        .eq("name",course.getAddress())
        );
        if(ObjectUtils.isEmpty(test_room)  ){
            return "FK_ERROR_1";
        }else if(ObjectUtils.isEmpty(test_teacher)){
            return "FK_ERROR_2";
        }
        UpdateWrapper<CourseEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", course.getId())
                .set("address", course.getAddress())
                .set("length", course.getLength())
                .set("teacher", course.getTeacher())
                .set("name",course.getName())
                .set("week",course.getWeek())
                .set("update_by",user.getUsername())
                .set("update_time", LocalDateTime.now());
        Integer rows = courseRepository.update(course, updateWrapper);
        if (!ObjectUtils.isEmpty(rows)) {
            return "token:更改成功！";
        }
        return null;
    }

    @Override
    public String updateMessageBoard(MessagesBoardEntity messagesBoard, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");

        MessagesBoardEntity master = messagesBoardRepository.selectOne(
                new QueryWrapper<MessagesBoardEntity>()
                        .eq("id", messagesBoard.getId())
        );
        if ("DBA".equals(user.getIdentity()) || master.getCreateBy().equals(user.getUsername())) {
            UpdateWrapper<MessagesBoardEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", messagesBoard.getId())
                    .set("text", messagesBoard.getText())
                    .set("update_by", user.getUsername())
                    .set("update_time", LocalDateTime.now());

            Integer rows = messagesBoardRepository.update(messagesBoard, updateWrapper);
            if (!ObjectUtils.isEmpty(rows)) {
                return "token:更改成功！";
            }
        }

        return null;
    }
}
